from tools.mongo_tool import MongoDbLink
import openpyxl
import random
import pymongo
import json
import os
import requests
from config.configs import GOAT_ERP_HOST, GOAT_ERP_PORT, DATA_PATH, DOWNLOAD_PATH

mongo_link = pymongo.MongoClient(GOAT_ERP_HOST, GOAT_ERP_PORT)
goat = mongo_link.goat_warehouse_dev


def login_goat_dev():
    """登录goat自提仓获取token"""
    r = requests.post(url='https://goat-dev-api.geehoop.com/auth/login',
                      data={"mobile": "17313699441", "password": "17313699441"})
    return r.json()['data']['token']


def get_file(limit, skip):
    """创建goat入库单xlsx文件"""
    mobile_list = [17313699441, 19071465376]
    email_list = [1596096369, 3328287875]
    a = random.randint(10000, 99001)
    uniquecode_list = ['D' + str(a + i) for i in range(limit)]
    # goatid_list = [i['_id'] for i in jw.find_limit('goatskus', {},limit_no=limit, skip_no=skip)]
    articlenumber_list = [i['sku'] for i in goat.goatskus.find({}).limit(limit).skip(skip)]
    data = [['唯一码', '货号', '尺码', '手机号', '邮箱', '类型']]
    for i in range(1, limit):
        data.append([uniquecode_list[i], articlenumber_list[i], random.randint(36, 44),
                     random.choice(mobile_list),
                     '{}@qq.com'.format(random.choice(email_list)), 'Goat自提'])
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = '库存'
    for row_index, row in enumerate(data):
        for col_index, value in enumerate(row):
            sheet.cell(row_index + 1, col_index + 1, value)
    file_path = 'E:/auto_script/stock/data/goat_upload_wait.xlsx'
    workbook.save(file_path)


def get_deposit_file(limit, skip):
    """创建goat寄存预约单xlsx文件"""
    articlenumber_list = [i['articleNumber'] for i in goat.find_limit('spus', {}, limit, skip)]
    merchantcode_list = [i['code'] for i in goat.merchants.find({})]
    data = [['货号', '规格', '数量', '商家编号']]
    for i in range(1, limit):
        data.append([articlenumber_list[i], random.randint(36, 44), random.randint(1, 10),
                     random.choice(merchantcode_list)])
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = '入库预约单'
    for row_index, row in enumerate(data):
        for col_index, value in enumerate(row):
            sheet.cell(row_index + 1, col_index + 1, value)
    file_path = 'E:/auto_script/goat_auto_scripts/datas/goat_depoist.xlsx'
    workbook.save(file_path)


def delete_stocks():
    """删除goatERP所有库存、付款数据"""
    goat.delete_many('stocks', {})
    goat.delete_many('payrecords', {})


def update_stocks_createdAt(condition, num1, num2, timestamp):
    """更改goat自提仓库存创建时间"""
    ids = [i['_id'] for i in
           goat.stocks.find(condition).limit(num1).skip(num2)]
    goat.stocks.update_many({"_id": {"$in": ids}}, {"$set": {"createdAt": timestamp}})


# update_stocks_createdAt({"articleNumber":"FJ3465 160","size":"42"},3,5,1706803742895)


def update_stocks_inStockTime(condition, num1, num2, timestamp):
    """更改goat自提仓库存入库时间"""
    ids = [i['_id'] for i in
           goat.stocks.find(condition).limit(num1).skip(num2)]
    goat.stocks.update_many({"_id": {"$in": ids}}, {"$set": {"inStockTime": timestamp}})


# update_stocks_inStockTime({"articleNumber":"FJ3465 160","size":"42","status":"waitingPay"},4,1,1706803742895)


def update_stocks_payTime(condition, num1, num2, timestamp):
    """更改goat自提仓库存入库时间"""
    ids = [i['_id'] for i in
           goat.stocks.find(condition).limit(num1).skip(num2)]
    goat.stocks.update_many({"_id": {"$in": ids}}, {"$set": {"payTime": timestamp}})


# update_stocks_payTime({"articleNumber":"FJ3465 160","size":"42","status":"stocked"},4,1,1706803742895)


def delete_payrecords(condition, num1, num2):
    """更改goat自提仓库存入库时间"""
    ids = [i['uniqueCode'] for i in
           goat.stocks.find(condition).limit(num1).skip(num2)]
    goat.payrecords.delete_many({"uniqueCode": {"$nin": ids}})
